import Cookie from 'js-cookie'
export default {
    state: {
        isCollapse: false,
        //面包屑数据
        tabList: [
            {

                path: "/",
                name: "home",
                label: "首页",
                icon: "s-home",
                url: "Home/Home",
            },
        ],
        menu:[]

    },
    mutations: {
        COLLAPSEMENU(state) {
            state.isCollapse = !state.isCollapse;
        },
        SELECTMENU(state,val){
            //判断添加数据是否为首页
            if(val.name !== 'home'){
                const index = state.tabList.findIndex(item=>item.name === val.name);
                // 如果不存在
                if(index === -1){
                    state.tabList.push(val);
                }
            }
        },
        CLOSETAG(state,item){
            const index = state.tabList.findIndex(val => val.name===item.name)
            state.tabList.splice(index,1);
        },
        setMenu(state,val){
            state.menu = val;
            Cookie.set('menu',JSON.stringify(val))
        },
        addMenu(state,router){
            if(!Cookie.get('menu')) return 
            const menu = JSON.parse(Cookie.get('menu'))
            state.menu = menu;

            //组装动态路由的数据
            const menuArray = []
            menu.forEach(item => {
                if(item.children){
                    item.children = item.children.map(item =>{
                        item.component = () => import(`../views/${item.url}`)
                        return item
                    })
                    menuArray.push(...item.children)
                }else{
                    item.component = () => import(`../views/${item.url}`)
                    menuArray.push(item)
                }
            });

            menuArray.forEach(item => {
                router.addRoute('Main',item)
            })
            
        }
    }
}